package com.service.impl;

import com.dao.BookMapper;
import com.dao.BrowRecoredMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.model.Book;
import com.model.BrowRecored;
import com.model.OrderRecored;
import com.model.User;
import com.service.BookService;
import com.util.BrowRecoredVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.sound.midi.Soundbank;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class BookServiceImpl implements BookService {
    @Autowired
    BookMapper bookMapper;

    @Autowired
    private BrowRecoredMapper browRecoredMapper;

    @Override
    public HashMap<String, Object> selectPage(Book book, String limit, String page) {
        HashMap<String,Object> map = new HashMap<>();
        PageHelper.startPage(Integer.parseInt(page),Integer.parseInt(limit));
        List<Book> bookList = bookMapper.selectPage(book);
        PageInfo<Book> pageInfo = new PageInfo<>(bookList);
        map.put("list",pageInfo.getList());
        map.put("total",pageInfo.getTotal());
        System.err.println("list"+pageInfo.getList()+"------------------s--------------------");
        System.err.println("total"+pageInfo.getTotal());
        return map;
    }

    @Override
    public HashMap<String, Object> selectPage2(String bookName, String varies, String limit, String page) {
        HashMap<String,Object> map = new HashMap<>();
        PageHelper.startPage(Integer.parseInt(page),Integer.parseInt(limit));
        List<Book> bookList = bookMapper.selectPage2(bookName,varies);
        PageInfo<Book> pageInfo = new PageInfo<>(bookList);
        map.put("list",pageInfo.getList());
        map.put("total",pageInfo.getTotal());
        return map;
    }




    //胡锦月
    //插入待管理员确定的借书记录
    @Override
    public int insertSelective(int userid, int bookId) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");//设置日期格式
        System.out.println(df.format(new Date()));// new Date()为获取当前系统时间

        BrowRecored browRecored=new BrowRecored();
        browRecored.setUserId(userid);
        browRecored.setBookId(bookId);
        DateFormat sdf=new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        String time= sdf.format( new Date());
        browRecored.setBrowDate(time);
        int i = browRecoredMapper.insertSelective(browRecored);
        return i;
    }
    //管理员确定该条借书记录
    @Override
    public int updateByBookState(int browRecoredId,String browDate) {
        BrowRecored browRecored=new BrowRecored();
        browRecored.setBrowDate(browDate);
        browRecored.setBrowRecoredId(browRecoredId);
        browRecored.setBookState(1);
        System.out.println("browRecored = ============================" + browRecored);
        int i = browRecoredMapper.updateByPrimaryKeySelective(browRecored);
        System.out.println("i ===================================== " + i);
        return i;
    }
    //查询所有待确定的借书记录
    @Override
    public HashMap<String,Object> queryForBrowList(String page ,String limit) {
        HashMap<String,Object> map = new HashMap<>();
        PageHelper.startPage(Integer.parseInt(page),Integer.parseInt(limit));
        List<BrowRecored> browRecoreds = browRecoredMapper.queryForBrowList();
        List<BrowRecoredVO> browRecoredVOS=new ArrayList<>();
        for (BrowRecored browRecored : browRecoreds) {
            BrowRecoredVO browRecoredVO=new BrowRecoredVO();
            System.out.println(browRecored.getBrowDate());
            browRecoredVO.setBookId(browRecored.getBookId());
            browRecoredVO.setBookName(browRecored.getBook().getBookName());
            browRecoredVO.setBrowRecoredId(browRecored.getBrowRecoredId());
            browRecoredVO.setUserId(browRecored.getUserId());
            browRecoredVO.setUsername(browRecored.getUser().getUsername());
            browRecoredVO.setBrowDate(browRecored.getBrowDate());
            browRecoredVOS.add(browRecoredVO);
        }
        PageInfo<BrowRecoredVO> pageInfo = new PageInfo<>(browRecoredVOS);
        map.put("list",pageInfo.getList());
        map.put("total",pageInfo.getTotal());
        return map;
    }
    //借书成功后，该书库存减一
    @Override
    public int updateStockNumberByBookId(int bookId) {
        Book book=bookMapper.selectByPrimaryKey(bookId);
        int num=book.getStockNumber();
        Book resultBook=new Book();
        resultBook.setStockNumber(num-1);
        resultBook.setBookId(book.getBookId());
        int i=bookMapper.updateByPrimaryKeySelective(resultBook);
        return i;
    }
    //查询历史借阅记录
    @Override
    public HashMap<String,Object> queryBeforeBrowBookList(int userid,String page,String limit) {
       /* List<BrowRecored> browRecoreds2=browRecoredMapper.queryBeforeBrowBookListByUserId(userid);*/
        HashMap<String,Object> map = new HashMap<>();
        PageHelper.startPage(Integer.parseInt(page),Integer.parseInt(limit));
        List<BrowRecored> browRecoreds=browRecoredMapper.queryBeforeBrowBookListByUserId(userid);
        System.out.println(browRecoreds);
        List<BrowRecoredVO> browRecoredVOS=new ArrayList<>();
        for (BrowRecored browRecored : browRecoreds) {
            BrowRecoredVO browRecoredVO=new BrowRecoredVO();
            System.err.println(browRecored.getBook());
            browRecoredVO.setBookId(browRecored.getBookId());
            browRecoredVO.setBookName(browRecored.getBook().getBookName());
            browRecoredVO.setBrowRecoredId(browRecored.getBrowRecoredId());
            browRecoredVO.setBrowDate(browRecored.getBrowDate());
            browRecoredVOS.add(browRecoredVO);
        }
        PageInfo<BrowRecoredVO> pageInfo = new PageInfo<>(browRecoredVOS);
        map.put("total",pageInfo.getTotal());
        map.put("list",pageInfo.getList());
        System.err.println("---------------------------------------------"+pageInfo.getTotal());
        return map;
    }
    //查询当前和历史借阅记录
    @Override
    public List<BrowRecored> queryNowAndBeforeBrowList() {
        List<BrowRecored> browRecoreds=browRecoredMapper.queryNowAndBeforeBrowList();
        return browRecoreds;
    }


    @Override
    public User queryUser() {
        return null;
    }

    @Override
    public List<BrowRecored> queryNowBrowListByUserId(Integer userId) {
        return browRecoredMapper.queryNowBrowListByUserId(userId);
    }

    @Override
    public List<BrowRecored> queryHandleListByUserId(Integer userId) {
        return browRecoredMapper.queryHandleListByUserId(userId);
    }

    @Override
    public Book selectByPrimaryKey(Integer bookId) {
        return bookMapper.selectByPrimaryKey(bookId);
    }

    @Override
    public int updateByPrimaryKeySelective(Book book_db) {
        return bookMapper.updateByPrimaryKeySelective(book_db);
    }

    //根据bookid查询当前图书的库存
    @Override
    public int queryBookNumber(int bookId) {
        Book book=bookMapper.selectByPrimaryKey(bookId);
        int bookNumber=book.getStockNumber();
        return bookNumber;
    }

    @Override
    public int insert(Book book) {
        bookMapper.insert(book);
        //获得数据库该书的id值
        Book book1  = bookMapper.selectByBookName(book.getBookName());
        return book1.getBookId();
    }

    @Override
    public int insertUpBook(int upBookId, int variesId) {
        return bookMapper.insertUpBook(upBookId,variesId);
    }
}
